function dateFormat(val) {
  if (!val) {
    return;
  }

  let date = new Date(val);
  let [Y, M, D] = [date.getFullYear(), date.getMonth() + 1, date.getDate()];
  let str = `${Y}-${M > 9 ? M : "0" + M}-${D > 9 ? D : "0" + D} `;
  return str;
}

function timeFormat(time, format) {
  // if (!time) return null
  // format = format != null ? format : 'YYYY年MM月DD日 HH:mm:ss'
  // return moment(time).format(format)
}
/* 格式货币 */
const digitsRE = /(\d{3})(?=\d)/g;

function qtyFormat(value, decimals) {
  value = parseFloat(value);
  if (!isFinite(value) || (!value && value !== 0)) return "";
  let lens = value.toString().split(".");
  decimals = decimals != null ? decimals : lens.length > 1 ? lens[1].length : 0;
  var stringified = Math.abs(value).toFixed(decimals);
  var _int = decimals ? stringified.slice(0, -1 - decimals) : stringified;
  var i = _int.length % 3;
  var head = i > 0 ? _int.slice(0, i) + (_int.length > 3 ? "," : "") : "";
  var _float = decimals ? stringified.slice(-1 - decimals) : "";
  var sign = value < 0 ? "-" : "";
  return sign + head + _int.slice(i).replace(digitsRE, "$1,") + _float;
}

function currency(value, decimals, currency) {
  value = parseFloat(value);
  if (!isFinite(value) || (!value && value !== 0)) return "";
  currency = currency != null ? currency : `￥`;
  decimals = decimals != null ? decimals : 2;
  var stringified = Math.abs(value).toFixed(decimals);
  var _int = decimals ? stringified.slice(0, -1 - decimals) : stringified;
  var i = _int.length % 3;
  var head = i > 0 ? _int.slice(0, i) + (_int.length > 3 ? "," : "") : "";
  var _float = decimals ? stringified.slice(-1 - decimals) : "";
  var sign = value < 0 ? "-" : "";
  return (
    sign + currency + head + _int.slice(i).replace(digitsRE, "$1,") + _float
  );
}

/* 格式化显示手机号 159 9999 9999 */
function telFormat(tel) {
  if (!tel) return "-";
  let startTel = tel.slice(0, 3);
  let midTel = tel.slice(3, 7);
  let endTel = tel.slice(7, 12);
  return `${startTel} ${midTel} ${endTel}`;
}

/* 格式化银行卡 1599 9999 9999 9999*/
function cardNumFormat(num) {
  if (!num) return "-";
  let startNum = num.slice(0, 4);
  let minTelNum = num.slice(4, 8);
  let midTel2Num = num.slice(8, 12);
  let endNum = num.slice(12, 16);
  return `${startNum} ${minTelNum} ${midTel2Num} ${endNum}`;
}

function formatDate(time, format) {
  return time.format(format);
}

// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子：
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
Date.prototype.format = function(fmt) {
  //author: meizz
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "H+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "w+": "星期" + "一二三四五六日".split("")[this.getDay() - 1], //星期
    S: this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(
      RegExp.$1,
      (this.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
  return fmt;
};

export default {
  dateFormat: dateFormat,
  timeFormat: timeFormat,
  telFormat: telFormat,
  cardNumFormat: cardNumFormat,
  currency: currency,
  qtyFormat: qtyFormat,
  formatDate: formatDate
};
